from sympy import Symbol, limit, oo, sin

if __name__ == '__main__':
    n = Symbol("n")

    f = (1 + n) * n / 2 / (n * n)
    print(f)
    result = limit(f, n, oo, dir="+-")
    print(result)

    x = Symbol("x")
    func = (sin(x) + x) / (3 * x + x ** 3)
    result = limit(func, x, 0, dir="+-")
    print(result)

    from sympy import symbols, Sum, limit, oo

    # 定义符号变量
    n = symbols('n', integer=True, positive=True)
    k = symbols('k')

    # 创建求和表达式
    sum_expr = Sum(k / n ** 2, (k, 1, n))

    # 计算求和结果
    sum_value = sum_expr.doit()  # 输出 (n + 1)/(2n)

    # 计算极限
    limit_value = limit(sum_value, n, oo)

    print("极限值为:", limit_value)  # 输出 1/2